﻿//
// DirectXPage.xaml.cpp
// Implementation of the DirectXPage.xaml class.
//

#include "pch.h"
#include "DirectXPage.xaml.h"
#include <ppltasks.h>
using namespace DropDown;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Graphics::Display;
using namespace Windows::UI::Input;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Core;
using namespace Windows::Storage::Streams;
using namespace Windows::Storage;
using namespace concurrency;
DirectXPage::DirectXPage()
{
	InitializeComponent();

	m_displayRect.Initialize(
		Window::Current->CoreWindow,
		SwapChainPanel,
		DisplayProperties::LogicalDpi
		);

	player.Width=40;
	player.Height=40;
	player.Create(m_displayRect.Context);

	leftSide.X=100;
	leftSide.Width=500;
	leftSide.Height=900;
	leftSide.RollYSpeed=15;
	leftSide.Create(m_displayRect.Context);

	rightSide.X=Window::Current->Bounds.Width-600;
	rightSide.Width=500;
	rightSide.Height=900;
	rightSide.RollYSpeed=5;
	rightSide.Create(m_displayRect.Context);

	task<StorageFile^>(KnownFolders::PicturesLibrary->GetFileAsync("1.jpg")).then([this](StorageFile^ file){
		player.SetSource(file);
		leftSide.SetSource(file);
		rightSide.SetSource(file);
	});

	m_displayRect.AppendDisplayItem(&player);
	m_displayRect.AppendDisplayItem(&leftSide);
	m_displayRect.AppendDisplayItem(&rightSide);
	i=0;
	Window::Current->CoreWindow->SizeChanged += 
		ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &DirectXPage::OnWindowSizeChanged);

	DisplayProperties::LogicalDpiChanged +=
		ref new DisplayPropertiesEventHandler(this, &DirectXPage::OnLogicalDpiChanged);

	DisplayProperties::OrientationChanged +=
		ref new DisplayPropertiesEventHandler(this, &DirectXPage::OnOrientationChanged);

	DisplayProperties::DisplayContentsInvalidated +=
		ref new DisplayPropertiesEventHandler(this, &DirectXPage::OnDisplayContentsInvalidated);

	m_eventToken = CompositionTarget::Rendering::add(ref new EventHandler<Object^>(this, &DirectXPage::OnRendering));

	m_timer = ref new BasicTimer();

	Window::Current->CoreWindow->KeyUp+=ref new TypedEventHandler<CoreWindow^,KeyEventArgs^>(this,&DirectXPage::Key_Up);
	Window::Current->CoreWindow->KeyDown+=ref new TypedEventHandler<CoreWindow^,KeyEventArgs^>(this,&DirectXPage::Key_Down);


}

void DirectXPage::Key_Up(CoreWindow^ sender,KeyEventArgs^ e)
{
	if(e->VirtualKey==Windows::System::VirtualKey::Left)
	{
		player.SetXSpeed(0);
	}
	else if(e->VirtualKey==Windows::System::VirtualKey::Right)
	{
		player.SetXSpeed(0);
	}
}

void DirectXPage::Key_Down(CoreWindow^ sender,KeyEventArgs^ e)
{
	if(e->VirtualKey==Windows::System::VirtualKey::Left)
	{
		player.SetXSpeed(-5);
	}
	else if(e->VirtualKey==Windows::System::VirtualKey::Right)
	{
		player.SetXSpeed(5);
	}
}


void DirectXPage::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
{
	m_displayRect.UpdateForWindowSizeChange();
}

void DirectXPage::OnLogicalDpiChanged(Object^ sender)
{
	m_displayRect.SetDpi(DisplayProperties::LogicalDpi);
}

void DirectXPage::OnOrientationChanged(Object^ sender)
{
	m_displayRect.UpdateForWindowSizeChange();
}

void DirectXPage::OnDisplayContentsInvalidated(Object^ sender)
{
	m_displayRect.ValidateDevice();
}

void DirectXPage::OnRendering(Object^ sender, Object^ args)
{
	player.Update();
	leftSide.Update();
	rightSide.Update();
	m_displayRect.Render();
	m_displayRect.Present();
}



void DropDown::DirectXPage::SwapChainPanel_Loaded_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{

}
